﻿"use strict";
define(['admin/app', "service/admin/shippingService", "angular-city-select"], function (app) {
    app.controller("shippingController", function ($scope, $state, $stateParams, utils, w5cValidator, baseService, shippingService) {
        $scope.list = {};
        $scope.model = {};
        $scope.query = {
            page: 1, size: 20, status: 0, bill_type: 0, order_no: '', start_date: '', end_date: ''
        };
        $.extend($scope.query, $stateParams);
        $scope.$on('onCitySelected', function (event, item) {
            $scope.model.region_id = item.id;
        });
        var methods = {
            search: function () {
                shippingService.gets($scope.query).success(function (response) {
                    $scope.list = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    shippingService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        $scope.model.region_id = response.data.region_id.toString();
                    });
                    $scope.breadcrumb = "编辑";
                } else {
                    $scope.breadcrumb = "新增";
                }
                //w5cValidator.setRules({
                //    name: { required: '请填写名称' },
                //    url: { required: '请填写Url' }
                //});
            },
            goBack: function () {
                history.back();
            },
            get_status: function (status) {
                var result = '';
                if (status === 1) {
                    result = '待发';
                    $scope.visible = true;
                }
                else if (status === 2) {
                    result = '发送';
                    $scope.visible = false;
                }
                return result;
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "确定要删除吗？", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    shippingService.delete(id).success(function () {
                        methods.search();
                    });
                });
            },
            send: function (id) {
                var modal = utils.confirm({ msg: "确定要发送吗？", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    shippingService.send({ id: id }).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000) {
                            methods.search();
                        }
                    });
                });
            },
            save: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                shippingService.save(model).success(function (response) {
                    // utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                    if (response.result_code == 1000) {
                        methods.goBack();
                    }
                });
            }
        };

        angular.extend($scope, methods);
        if ($state.includes('app.order.position_detail')) {
            methods.edit();
        } else {
            methods.search();
            //baseService.region();
        }
    });
});
